﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace fhjlForm
{
    public partial class Form_Main : Form
    {
        bool bTextTrack = true;
        public Form_Main()
        {
            InitializeComponent();
            toolStripButton2.ForeColor = Color.Red;
            textBox1.MaxLength = Int32.MaxValue;
            try
            {
                ComWithWeb.PortName = Properties.Settings.Default.ComPortWithWeb;
                ComWithWeb.Open();
                toolStripButton3.Text = "关闭远程控制";
                toolStripButton4.Enabled = false;
            }
            catch
            {
                MessageBox.Show("远程通信串口无法打开，请检测系统环境");
                toolStripButton3.Text = "打开远程控制";
                toolStripButton4.Enabled = true;
            }
            toolStripStatusLabel2.Text = toolStripButton3.Text;
            qidongfuwu();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            bTextTrack = !bTextTrack;
            toolStripButton2.Checked = bTextTrack;
            if (bTextTrack)
                toolStripButton2.ForeColor = Color.Red;
            else
                toolStripButton2.ForeColor = Color.FromName(KnownColor.ControlText.ToString());
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (toolStripButton3.Text == "打开远程控制")
            {
                try
                {
                    ComWithWeb.PortName = Properties.Settings.Default.ComPortWithWeb;
                    ComWithWeb.Open();
                    toolStripButton3.Text = "关闭远程控制";
                    toolStripButton4.Enabled = false;
                }
                catch
                {
                    MessageBox.Show("远程通信串口无法打开，请检测系统环境");
                    toolStripButton3.Text = "打开远程控制";
                    toolStripButton4.Enabled = true;
                }
            }
            else
            {
                ComWithWeb.Close();
                toolStripButton3.Text = "打开远程控制";
                toolStripButton4.Enabled = true;
            }
            toolStripStatusLabel2.Text = toolStripButton3.Text;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Form_ComSetting f = new Form_ComSetting();
            f.ShowDialog();
        }
        public void AddText(string str)
        {
            if (textBox1.Text.Length >= textBox1.MaxLength)
                textBox1.Text = "";
            
            DateTime dt = DateTime.Now;
            textBox1.AppendText(dt.ToString() + ":     " + str + "\r\n");            

            string filenames = String.Format("{0:D4}", dt.Year) + "-" + String.Format("{0:D2}", dt.Month) + "-" + String.Format("{0:D2}", dt.Day) + "运行信息.txt";

            FileStream fs = new FileStream(filenames, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            fs.Seek(0, SeekOrigin.End);

            sw.WriteLine(dt.ToString() + ":     " + str);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();/**/
        }

        private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            tingzhifuwu();
        }       
    }
}
